package ciyuan.Item.SkillBook;

import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ArmorStandEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;

/**
 * @author 150149
 */
public class Starshards extends SkillBookType {

    private static final String SUBSTRING = ",starCount";

    public Starshards() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 10 * 1000);

        SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    if (SkillUtils.getSaveData(playerEntity, SUBSTRING) < 2) {
                        return;
                    }
                    SkillUtils.setSaveData(playerEntity,SUBSTRING,SkillUtils.getSaveData(playerEntity,SUBSTRING)-2);
                    BlockPos blockPos = SkillUtils.getPlayerSee(playerEntity.level,playerEntity, RayTraceContext.FluidMode.SOURCE_ONLY,3).getBlockPos();
                    for(LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(
                            blockPos.getX()+7,blockPos.getY()+7,blockPos.getZ()+7,blockPos.getX()-7,blockPos.getY()-7,blockPos.getZ()-7
                    ))) {
                        if (livingentity != playerEntity && !playerEntity.isAlliedTo(livingentity) && (!(livingentity instanceof ArmorStandEntity) || !((ArmorStandEntity)livingentity).isMarker()) && blockPos.distSqr(livingentity.getX(),livingentity.getY(),livingentity.getZ(),false) < 18.0D) {
                            if (playerEntity.level instanceof ServerWorld) {
                                livingentity.addEffect(new EffectInstance(Effects.MOVEMENT_SLOWDOWN, 100, 3));
                                livingentity.hurt(DamageSource.playerAttack(playerEntity), 10);
                                double d0 = -MathHelper.sin(playerEntity.yRot * ((float) Math.PI / 180F));
                                double d1 = MathHelper.cos(playerEntity.yRot * ((float) Math.PI / 180F));
                                ((ServerWorld) playerEntity.level).sendParticles(ParticleTypes.SWEEP_ATTACK, livingentity.getX() + d0, livingentity.getY(), livingentity.getZ() + d1, 0, d0, 0.0D, d1, 0.0D);
                                playerEntity.level.playSound(null, playerEntity.getX(), playerEntity.getY(), playerEntity.getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, playerEntity.getSoundSource(), 1.0F, 1.0F);
                            }
                        }
                    }
                    SkillUtils.sleep(300);
                }
            }
        });

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "Starshards";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.11");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.11");
    }

}
